/*
 * EvaluateFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.service.feedback;

import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.utils.UIUtils;

/** 意见反馈 */
public class FeedbackFragment extends BaseFragment
        implements FeedbackContract.View {

    private static final String TAG = FeedbackFragment.class.getSimpleName();
    private Toolbar mToolbar;
    private FeedbackContract.Presenter mPresenter;
    private TextView mTvSure;
    private EditText mEtInfo;
    private LoadingDialog mLoadingDialog;
    private EditText mEtConnection;

    public static FeedbackFragment getInstance() {
        return new FeedbackFragment();
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvSure = (TextView) mRootView.findViewById(R.id.tv_sure);
        mEtInfo = (EditText) mRootView.findViewById(R.id.et_info);
        mEtConnection = (EditText) mRootView.findViewById(R.id.et_connection);

        initEvent();
    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mEtInfo.addTextChangedListener(
                new TextWatcher() {
                    @Override
                    public void beforeTextChanged(
                            CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        String info = getFeedBackInfo();
                        String connect = getConnection();
                        mTvSure.setEnabled(!TextUtils.isEmpty(info) || !TextUtils.isEmpty(connect));
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
        mEtConnection.addTextChangedListener(
                new TextWatcher() {
                    @Override
                    public void beforeTextChanged(
                            CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        String info = getFeedBackInfo();
                        String connect = getConnection();
                        mTvSure.setEnabled(!TextUtils.isEmpty(info) && !TextUtils.isEmpty(connect));
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
        mTvSure.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /* showDialog();
                        String feedback = getInfo();
                        mConversation.addUserReply(feedback);
                        mConversation.sync(new SyncListener() {
                            @Override
                            public void onReceiveDevReply(List<Reply> list) {
                                Log.i("GOOD", "*onReceiveDevReply**" + list.toString());
                            }

                            @Override
                            public void onSendUserReply(List<Reply> list) {
                                disMissDialog();
                                UIUtils.showToast("提交成功");
                                mActivity.finish();
                            }
                        });*/
                        mPresenter.start();
                    }
                });
    }

    @Override
    public String getConnection() {
        return mEtConnection.getText().toString().trim();
    }

    @Override
    public String getFeedBackInfo() {
        return mEtInfo.getText().toString().trim();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_feedback;
    }

    @Override
    public void setPresenter(FeedbackContract.Presenter presenter) {

        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void showDialog() {
        mLoadingDialog = new LoadingDialog(mActivity);
        mLoadingDialog.show();
    }

    @Override
    public void disMissDialog() {
        mLoadingDialog.dismiss();
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }
}
